#include<stdio.h>
//#include<conio.h>
int *a,n,i,j,k; //global variables
void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);
int main()
{
    	int ch;
    	app();
    	printf("enter your choice");
    	scanf("%d",&ch);
	{
		printf("\nlist functions using arrays");
		printf("\n1. reversing array");
		printf("\n2. counting number of elements in array");
		printf("\n3. searching in array");
		printf("\n4. sorting the array");
		printf("\n5. delete");
		printf("\n6. exit \n");
		printf("\nenter your choice=");
		scanf("%d",&ch); 
		switch (ch)
		{
			case 1: rev();
			break;
			case 2: count();
			break;
			case 3: search();
			break;
			case 4:sort();
			break;
			case 5: delete1();
			break;
			case 6: 
			break;
			default: printf("enter a valid choice");
		}
	}
}
int rev()
{
	app();
	for(i=0;i<n;i++)
	{
		printf(" %d ",a[i]);
	}
	printf("\n reverse of array=\n ");
	for(j=n;j>0;j--)
	{
		printf(" %d ",a[--i]);
	}
	return 0;
}
int count()
{
	app();
	printf("number of elements in array= %d \n",n);
	return 0;
}

int search()
{
	app();
	int elem,temp=0;
	printf("Enter the element to be search\n");
     	scanf("%d",&elem);
	for (i=0; i<n; i++)
     	{
         	if (a[i]==elem)
         	{
          		 temp=1;
          		 printf("Element found %d , position=%d\n",elem,i);
         	}
      	}
       	if (!temp)
     		printf("Element not found\n");  
 		return 0;   
}
void quicksort(int a[], int low, int high)
{
	int middle;
	if (low >= high) 
	return;
	middle = split(a, low, high);
	quicksort(a, low, middle - 1);
	quicksort(a, middle + 1, high);
}
int split(int a[], int low, int high)
{
	int part_element = a[low];
	for (;;) 
	{
		while (low < high && part_element <= a[high])
		high--;
		if (low >= high) break;
		a[low++] = a[high];
		while (low < high && a[low] <= part_element)
		low++;
		if (low >= high) 
		break;
		a[high--] = a[low];
	}
	a[high] = part_element;
	return high;
}
int sort()
{
	app();
	quicksort(a, 0, n - 1);
	printf("In sorted order: ");
	for (i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
		//printf("\n");
	}
	return 0;
}
int app()
{	i=0;
	char cho='y';
	while(cho=='y')
	{	
		a=(int *)realloc(a,i+1*sizeof(int));
		printf("enter element=");
		scanf("%d", &a[i]);
		i=i+1;
		printf("if you want to enter more press y else any other key to stop...");
		scanf("%s",&cho);
	}
	n=i;
}
int delete1()
{
	int pos;
	app();
	printf("\nEnter position where to delete: ");
	scanf("%d",&pos);
	i=0;
	while (i<pos)
	i++;
	while(i<n)
	{
		a[i]=a[i+1];
		i++;

	}
	i=0;
	--n;
	while(i<n)
	{
		printf("%d ",a[i++]);
	}
	return 0;
}

